use "./Merged dataset.dta"
graph set window fontface "Gill Sans"
replace cname="England" if cname=="United Kingdom"
label define group 0 "Poor" 1 "Middle" 2 "Rich"
drop if situation==6 | situation==11
set scheme lean2

global fspecssc legend(off) graphregion(margin(zero))
global fspecsscl graphregion(margin(zero))
global fspecsx xtitle("") xscale(range(1800 2022)) xlabel(1800 (20) 2020)
global fspecsx xtitle("") xscale(range(1800 2022)) xlabel(1800 (20) 2020)
global fspecs $fspecssc $fspecsx

*********
*Figure 2

preserve

collapse (mean) some, by(cname year situation group)
collapse (count) some, by(cname year group)
sort cname year group

local pos_England_p 12.8 1840 "Poor"
local pos_England_m 5.8 1870 "Middle"
local pos_England_r 5.8 1930 "Rich"
local pos_Sweden_p 12.8 1840 "Poor"
local pos_Sweden_m 7.8 1870 "Middle"
local pos_Sweden_r 8.8 1930 "Rich"
local tit_England "(A) England"
local tit_Sweden "(B) Sweden"

foreach c in "England" "Sweden" {
twoway ///
(line some year if group == 0 & cname == "`c'", lcolor(cranberry) lpattern(_)) /// 
(line some year if group == 1 & cname == "`c'", lcolor(black) lpattern(.)) /// 
(line some year if group == 2 & cname == "`c'", lcolor(edkblue) lpattern(solid)), ///
ytitle("Number of Situations", size(19 pt)) ///
text(`pos_`c'_p', color(cranberry) size(18 pt)) ///
text(`pos_`c'_m', color(black) size(18 pt)) ///
text(`pos_`c'_r', color(edkblue) size(18 pt)) ///
xtitle(`tit_`c'', size(24 pt)) ///
yscale(range(0 13)) ylab(0 5 10 13,nogrid norescale) xscale(range(1800 2022) titlegap(5)) xlab(1800 (20) 2020) legend(off) xlabel(,labsize(17 pt)) ylabel(,labsize(17 pt))
graph save "`name'_`c'", replace
}

graph combine _England.gph _Sweden.gph, imargin(small) rows(1) xsize(12) ysize(5)
graph save Figure_2, replace

restore

******
*Figure 3

preserve 

local name Selective

local avars subgroup official office_name place benefit_name

local checkvars official_n office_name_n discretion level church voluntary place_n benefit_name_n

foreach v in `avars' {
    encode `v', gen(`v'_n)
}

foreach v in `checkvars' {
    replace `v' = . if some == .	
}

bysort cname year situation cashkind: egen selective = count(some)

gen nopol = 0
replace nopol = 1 if selective == 0

gen unipol = 0
replace unipol = 1 if selective == 3

collapse (mean) nopol unipol (semean) `checkvars' (mean) some, by(cname year situation)

foreach v in nopol unipol {
    replace `v' = 0 if `v' > 0 & `v' < 1 	
}

foreach v in `checkvars' {    
	replace unipol = 0 if `v' > 0 & `v' != .
}

replace unipol = 0 if some > 0 & some != .

generate selpol = 0
replace selpol = 1 if nopol == 0 & unipol == 0

collapse (sum) nopol selpol unipol, by(cname year)

local tit_England "(A) England"
local tit_Sweden "(B) Sweden"

foreach c in "England" "Sweden" {

graph bar unipol selpol if cname == "`c'", over(year) stack bar(2, color(black)) ///
scale(1.4) b1title("  ") b2title(`tit_`c'', size(18 pt)) /// 
ytitle("Universal and Selective Policies") ///
yscale(range(0 13)) ylab(0 5 10 13,nogrid norescale) legend(off)

graph save "`name'_`c'", replace

}

graph combine Selective_England.gph Selective_Sweden.gph, imargin(small) rows(1) xsize(12) ysize(5)
graph save Figure_3, replace

restore

*********
*Figure 4

preserve
*Don't count employers twice
replace office_name="" if official=="employer"
by cname year official office_name, sort: generate unique = (_n == 1) * (some !=.)
collapse (sum) unique, by(cname year)
separate unique, by(cname) veryshortlabel
replace unique1=-unique1
twoway bar unique1 year, barwidth(7) || bar unique2 year, barwidth(7) yscale(reverse) fcolor(gs0) ///
$fspecs ///
ytitle("Number of Different Officials") ///
ylabel(-20 "20" -15 "15" -10 "10" -5 "5" 0(5)20 , nogrid) ///
xlabel(#12, nogrid) ///
text(16 1910 "Sweden") text(-18.2 1910 "England", color(gs8))
graph save Figure_4, replace
restore

*********
*Figure 5

preserve

local name Officialsbygroup
local pos_England_p 0.6 1930 "Poor"
local pos_England_m .95 1915 "Middle"
local pos_England_r 1.13 1910 "Rich"
local pos_Sweden_p 0.46 1885 "Poor"
local pos_Sweden_m 1.17 1910 "Middle"
local pos_Sweden_r 0.95 1890 "Rich"
local tit_England "(A) England"
local tit_Sweden "(B) Sweden"

*Fix some coding errors for the backdate (see codebook)
replace cashkind=1 if cname=="England" & year==1980 & group==2 & situation==4 & cashkind==0
replace cashkind=0 if cname=="England" & year==2020 & group==0 & situation==14
replace cashkind=0 if some==. & cashkind==.
*Backdate the cash-kind split
expand 2 if cashkind==0, generate(backdate)
gen covered =1 if some != .
*Don't count employers twice
replace office_name="" if official=="employer"
by cname year group official office_name, sort: generate officialcount = (_n == 1) * (some != .)
collapse (sum) officialcount covered, by (cname year group)
*Divide coverage by two so the denominator is situations
gen officials_perinteraction = officialcount/(covered/2)

foreach c in "England" "Sweden" {
twoway ///
(line officials_perinteraction year if group == 0 & cname == "`c'", lcolor(cranberry) lpattern(_)) ///
(line officials_perinteraction year if group == 1 & cname == "`c'", lcolor(black) lpattern(.)) /// 
(line officials_perinteraction year if group == 2 & cname == "`c'", lcolor(edkblue) lpattern(solid)), ///
ytitle("Number of Officials per Situation", size(19 pt)) ///
text(`pos_`c'_p', color(cranberry) size(18 pt)) ///
text(`pos_`c'_m', color(black) size(18 pt)) ///
text(`pos_`c'_r', color(edkblue) size(18 pt)) ///
xtitle(`tit_`c'', size(24 pt)) ///
yscale(range(0.2 1.2)) ylab(#6,nogrid) xscale(range(1800 2022) titlegap(5)) xlabel(1800 (20) 2020) legend(off) xlabel(,labsize(17 pt)) ylabel(,labsize(17 pt))
graph save "`name'_`c'", replace
}

graph combine Officialsbygroup_England.gph Officialsbygroup_Sweden.gph, imargin(small) rows(1) xsize(12) ysize(5)
graph save Figure_5, replace

restore

*********
*Figure 6

preserve
gen levlocal=1 if level==0
gen levregional=1 if level==1
gen levcentral=1 if level==2
collapse (count) levlocal levregional levcentral, by (cname year)
graph bar levlocal levregional levcentral, over(cname, label(angle(vertical) labsize(vsmall))) over(year) ///
bar(1, fcolor(gs13)) bar(2, fcolor(gs16)) bar(3, fcolor(gs0)) stack ///
legend(col(3) position(11) ring(0) size(small) lab (1 "Local") lab(2 "Regional") lab(3 "Central")) ///
$fspecsscl ylabel(,nogrid) ///
ytitle("Number of Interactions")
graph save Figure_6, replace
restore

*********
*Figure 7

preserve
gen levlocal=1 if level==0
gen levregional=1 if level==1
gen levcentral=1 if level==2
collapse (count) levlocal levregional levcentral, by (cname year group)
drop if cname=="England"
label values group group
graph bar levlocal levregional levcentral, over(group, label(angle(vertical) labsize(small))) over(year) bar(1, fcolor(gs13)) bar(2, fcolor(gs16)) bar(3, fcolor(gs0)) stack $fspecsscl ylabel(,nogrid) ///
legend(off) ytitle("Number of Interactions" "in Sweden")
graph save Fig7_SWE, replace
restore

preserve
gen levlocal=1 if level==0
gen levregional=1 if level==1
gen levcentral=1 if level==2
collapse (count) levlocal levregional levcentral, by (cname year group)
drop if cname=="Sweden"
label values group group
graph bar levlocal levregional levcentral, over(group, label(angle(vertical) labsize(small))) over(year) bar(1, fcolor(gs13)) bar(2, fcolor(gs16)) bar(3, fcolor(gs0)) stack $fspecsscl ylabel(,nogrid) ///
legend(bmargin(medium) col(3) position(11) ring(0) size(small) lab(1 "Local") lab(2 "Regional") lab(3 "Central")) ///
ytitle("Number of Interactions" "in England")
graph save Fig7_ENG, replace
restore

graph combine Fig7_ENG.gph Fig7_SWE.gph, col(1)
graph save Figure_7, replace

*********
*Figure 9

preserve
*Fix place for england-sick-2000 (see coding report)
replace place="1,3,4,6" if cname=="England" & year==2000 & situation==2 & group==0
gen home=1 if strpos(place,("1"))
gen officials_home=1 if strpos(place,("2"))
gen officials_station=1 if strpos(place,("3"))
gen correspondence=1 if strpos(place,("4"))
gen web=1 if strpos(place,("5"))
gen phone=1 if strpos(place,("6"))
gen work=1 if strpos(place,("7"))
gen public=1 if strpos(place,("8"))
*Avoid double count (see details below #1)
replace officials_home=. if home==1
replace home=. if officials_station==1
replace home=. if phone==1
replace correspondence=. if web==1
replace correspondence=. if phone==1
replace officials_station=. if officials_home==1
replace officials_home=. if public==1
replace correspondence=. if officials_station==1
replace web=. if officials_station==1
replace web=. if home==1
replace phone=. if officials_station==1
*phoneweb (see below #2)
gen phoneweb=1 if phone==1
replace phoneweb=1 if web==1
replace phone=. if phoneweb==1
replace web=. if phoneweb==1
collapse (count) home officials_home officials_station public correspondence work phoneweb, by(cname year)
graph bar home officials_home public officials_station work correspondence phoneweb, over(cname, label(angle(vertical) labsize(vsmall))) over(year) ///
bar(1, fcolor(gs0)) bar(2, fcolor(gs15)) bar(3, fcolor(gs9)) bar(4, fcolor(gs12)) bar(5, fcolor(gs16)) bar(6, fcolor(gs3)) bar(7, fcolor(gs7))  stack ///
legend(col(2) position(6) size(small) lab(1 "At home") lab(2 "At the official's home") lab(3 "In a public setting") lab(4 "At the official's station") lab(5 "At work") lab(6 "Via correspondence") lab(7 "By phone or on a website")) ///
$fspecsscl ytitle("Number of Interactions") ylabel(,nogrid)
graph save Figure_9, replace
restore

*1. So that we don't 'double count' interactions, when there are multiple 'place' entries I privilege:
	*home over officials home before 1900 in Sweden (8 observations, see coding report home treatment "common")
	*official's station over home after 1900 in Sweden (5 observations, see coding report clinics become more dominant)
	*official's home over station in England for registering births between 1880 and 1960 (15 observations, see coding report still often took place in dwellings in the 1950s)
	*public setting over official's home in England for middle class and rich victims of crime between 1800 and 1840 (6 observations)
	*official's station over correspondence for rich and middle physical illness in England in 1960 (2 observations, consistent with other years)
	*officials station over web for unemployment benefits in England in 2020 (3 observations, see coding report required an office visit to access)
	*home over web in England in 2020 (1 observations, see coding report home visit "typical")
	*phone over standard correspondence in 2000 and 2020 (5 observations)
	*web over standard correspondence in 2000 and 2020 (3 observations)
	*official's station over phone in Sweden in 2000 (3 observations, see coding report regarding tax issues to be dealt with at local office)
	
*2. We create a new category phoneweb where we count all phone and website interactions, to reduce clutter and because they often occur together (12 observations)